import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 读取Excel文件，假设Excel文件中包含多个Sheet
xls = pd.ExcelFile('C:/Users/zlsjBIDF/Downloads/例子.xlsx')
df = pd.read_excel(xls)
# 列名列表
column_names = ['landing_type', 'ad_type', 'is_comment_disable', 'ad_download_status', 'stat_cost', 'show_cnt', 'click_cnt', 'convert_cnt']
data_names = ['landing_type', 'ad_type', 'is_comment_disable', 'ad_download_status']
# 使用列表来选择DataFrame中的列
data = df[column_names]
df_encoded = pd.get_dummies(data, columns=['landing_type', 'ad_type', 'is_comment_disable', 'ad_download_status'])
df_encoded= df_encoded.fillna(0)
data = df_encoded.values
jisuan = np.empty((50000, 2))
for i in range(50000):
    if data[i][1] == 0:
        jisuan[i][0] = 0
    else:
        jisuan[i][0] = data[i][2] / data[i][1]
    if data[i][2]==0:
        jisuan[i][1] = 0
    else:
        jisuan[i][1]=data[i][3]/data[i][2]
label = np.empty(50000)
jishu = 0
for x in jisuan:
    if x[0]>0.05 or x[1]>0.05:
        label[jishu]=1
    else:
        label[jishu]=0
    jishu = jishu + 1
data = df[data_names]
data = pd.get_dummies(data, columns=['landing_type', 'ad_type', 'is_comment_disable', 'ad_download_status'])
data = data.values
X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.2, random_state=42)
model = LogisticRegression(max_iter=200)

# 训练模型
model.fit(X_train, y_train)

# 预测测试集
y_pred = model.predict(X_test)

# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision for class '1': {precision:.2f}")